package com.coocaa.app.lib.cleaner.scanner.config.impl;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.app.lib.cleaner.scanner.config.ConfigDataFactory;
import com.coocaa.app.lib.cleaner.scanner.config.ConfigManagerFactory;
import com.coocaa.app.lib.cleaner.scanner.config.data.CloudConfigData;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by lu on 17-4-17.
 */

public class CloudConfigLoader implements ConfigManagerFactory.ConfigLoader {

    public interface Loader {
        String load();
    }

    public static Loader loader = null;


    public static class FileDelKeepData implements Serializable {
        public List<String> keep = new ArrayList<String>();
        public List<String> del = new ArrayList<String>();
    }


    public CloudConfigLoader() {

    }

    @Override
    public void init(Context context) {

    }

    @Override
    public ConfigDataFactory.ConfigData load() {
        String value = null;
        if (loader != null)
            value = loader.load();
        return convert(value);
    }


    static ConfigDataFactory.ConfigData convert(String value) {
        Log.d("SCAN", "CloudConfigLoader convert:" + value);
        ConfigDataFactory.ConfigData data = new ConfigDataImpl();
        try {
            if (!TextUtils.isEmpty(value)) {
                JSONObject object = JSONObject.parseObject(value);
                String dataValue = object.getString("data");
                CloudConfigData configData = JSONObject.parseObject(dataValue, CloudConfigData.class);
                if (configData != null) {
                    String sdcardRoot = "";
                    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                        sdcardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
                    Log.d("SCAN","sdcardRoot:"+sdcardRoot);
                    List<String> ignoreItem = configData.ignorePackage;
                    if(ignoreItem != null && ignoreItem.size() > 0)
                    {
                        for(String pkg:ignoreItem) {
                            Log.d("SCAN","ignorePackage  pkg:"+pkg);
                            data.appendIgnorePackage(pkg);
                        }
                    }
                    CloudConfigData.ConfigItem<CloudConfigData.Item> packageDelItem = configData.packageDel;
                    if(packageDelItem != null){
                        if(!TextUtils.isEmpty(sdcardRoot) && packageDelItem.sdcard != null && packageDelItem.sdcard.size() > 0){
                            for(CloudConfigData.Item item:packageDelItem.sdcard){
                                Log.d("SCAN","packageDel sdcard pkg:"+item.pkg+" "+item.path);
                                if(item != null && !TextUtils.isEmpty(item.path)) {
                                    if (item.path.startsWith("/"))
                                        data.appendPackageDel(item.pkg, sdcardRoot + item.path);
                                    else
                                        data.appendPackageDel(item.pkg, sdcardRoot + "/" + item.path);
                                }
                            }
                        }
                        if(packageDelItem.data != null && packageDelItem.data.size() > 0){
                            for(CloudConfigData.Item item:packageDelItem.data){
                                Log.d("SCAN","packageDel data  pkg:"+item.pkg+" "+item.path);
                                data.appendPackageDel(item.pkg,item.path);
                            }
                        }
                    }
                    CloudConfigData.ConfigItem<CloudConfigData.Item> packageKeepItem = configData.packageKeep;
                    if(packageKeepItem != null){
                        if(!TextUtils.isEmpty(sdcardRoot) && packageKeepItem.sdcard != null && packageKeepItem.sdcard.size() > 0){
                            for(CloudConfigData.Item item:packageKeepItem.sdcard){
                                Log.d("SCAN","packageKeep sdcard  pkg:"+item.pkg+" "+item.path);
//                                data.appendPackageKeep(item.pkg,item.path);
                                if(item != null && !TextUtils.isEmpty(item.path)) {
                                    if (item.path.startsWith("/"))
                                        data.appendPackageKeep(item.pkg, sdcardRoot + item.path);
                                    else
                                        data.appendPackageKeep(item.pkg, sdcardRoot + "/" + item.path);
                                }
                            }
                        }
                        if(packageKeepItem.data != null && packageKeepItem.data.size() > 0){
                            for(CloudConfigData.Item item:packageKeepItem.data){
                                Log.d("SCAN","packageKeep data  pkg:"+item.pkg+" "+item.path);
                                data.appendPackageKeep(item.pkg,item.path);
                            }
                        }
                    }
                    CloudConfigData.ConfigItem<String> fileDelItem = configData.fileDel;
                    if(fileDelItem != null){
                        if(!TextUtils.isEmpty(sdcardRoot) && fileDelItem.sdcard != null && fileDelItem.sdcard.size() > 0){
                            for(String item:fileDelItem.sdcard){
                                Log.d("SCAN","fileDel sdcard  item:"+item);
//                                data.appendFileDel(item);
                                if(!TextUtils.isEmpty(item)) {
                                    if (item.startsWith("/"))
                                        data.appendFileDel(sdcardRoot + item);
                                    else
                                        data.appendFileDel(sdcardRoot + "/" + item);
                                }
                            }
                        }
                        if(fileDelItem.data != null && fileDelItem.data.size() > 0){
                            for(String item:fileDelItem.data){
                                Log.d("SCAN","fileDel data  item:"+item);
                                data.appendFileDel(item);
                            }
                        }
                    }
                    CloudConfigData.ConfigItem<String> fileKeepItem = configData.fileKeep;
                        if(fileKeepItem != null){
                        if(!TextUtils.isEmpty(sdcardRoot) && fileKeepItem.sdcard != null && fileKeepItem.sdcard.size() > 0){
                            for(String item:fileKeepItem.sdcard){
                                Log.d("SCAN","fileKeep sdcard  item:"+item);
//                                data.appendFileKeep(item);
                                if(!TextUtils.isEmpty(item)) {
                                    if (item.startsWith("/"))
                                        data.appendFileKeep(sdcardRoot + item);
                                    else
                                        data.appendFileKeep(sdcardRoot + "/" + item);
                                }
                            }
                        }
                        if(fileKeepItem.data != null && fileKeepItem.data.size() > 0){
                            for(String item:fileKeepItem.data){
                                Log.d("SCAN","fileKeep data  item:"+item);
                                data.appendFileKeep(item);
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }
}
